home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 707 < prev    next >
Internet Message Format  |  1996-08-06  |  6KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.std.c
  4. Subject: Re: Restrictions on qsort compare function?
  5. Date: Mon, 08 Apr 96 13:56:53 GMT
  6. Organization: none
  7. Message-ID: <828971813snz@genesis.demon.co.uk>
  8. References: <4iokop$h4p@lyra.csx.cam.ac.uk> <4iqjar$2m9@usenet.pa.dec.com> <4jgltp$f9l@lyra.csx.cam.ac.uk> <828644274snz@genesis.demon.co.uk> <4k28t4$2g0@sam.inforamp.net> <828726582snz@genesis.demon.co.uk> <4k69bf$ehg@sam.inforamp.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4k69bf$ehg@sam.inforamp.net>
  15.            pcurran@inforamp.net "Peter Curran" writes:
  16.  
  17. >On Fri, 05 Apr 96 17:49:42 GMT in article <828726582snz@genesis.demon.co.uk>
  18. >    Lawrence Kirby <fred@genesis.demon.co.uk> (Lawrence Kirby) wrote:
  19. >
  20. >>If you can find any definition as to what the behaviour should be with your
  21. >>comparison function, explain what it is. Otherwise the behaviour is
  22. >>undefined.
  23. >
  24. >IMHO, qsort() is required to return an array that is sorted according to the
  25. >criteria implied by the comparison function.  That is, at the point where qsort
  26. >returns, the array must match the order implied by the comparison function.
  27.  
  28. But if the comparison function is inconsistent it implies no order at all.
  29. If there is no ordering that is consistent with the values returned by the
  30. comparison function then clearly your requirement cannot be fulfilled.
  31.  
  32. > If
  33. >the comparison function definition is such that the order changes (although the
  34. >*definition* of the order must remain fixed), then IMHO the current wording of
  35. >the standard can be read as meaning that it is qsort's problem to deal with it.
  36.  
  37. No, qsort() is defined as a sorting function and what you describe is not
  38. a valid sorting operation.
  39.  
  40. >This all hinges on how one interprets the word "considered" in the standard -
  41.  
  42. The critical word is 'sorted'. I suggest you read section 5 (i.e. the
  43. first section of chapter 5) in Knuth Vol 3 to get a reasonable idea of what
  44. a sort is. I repeat that it is not the job of the C standard to define
  45. basic computer science terms. Particularly relevant:
  46.  
  47. "An ordering relation ``<'' is specified on the keys so that, for any three
  48.  key values a, b, c, the following conditions are satisfied:
  49.  
  50.  i)  Exactly one of the possibilities a < b, a = b, b < a is true. (This is
  51.      the law of trichotomy.)
  52.  
  53.  ii) If a < b and b < c, then a < c. (The law of transitivity.)
  54.  
  55. ...
  56.  
  57.   The goal of sorting is to determine a permutation p(1)p(2)...p(N) of
  58. the records, which puts the keys in nondescending order:
  59.  
  60.          K     <= K     <= ... <= K 
  61.           p(1)     p(2)            p(N)
  62. "
  63.  
  64. What you suggest above is not a valid ordering relation so cannot be used
  65. in a sort.
  66.  
  67. >that is a very vague term to use in a standard, and IMHO can reasonably lead to
  68. >confusion.  If a "snapshot" is taken at the time qsort() returns, the array
  69. > must
  70. >match order implied by the comparison function.  If the definition of the
  71. >comparison criteria cannot provide a definite sort order for the array at any
  72. >instant, then I agree that it is invalid, but if it does provide such an
  73. >definite order at any instant, then I think the current standard permits it.
  74.  
  75. What do you mean 'at any instant'? The relation doesn't have license to
  76. change during the sort operation.
  77.  
  78. >Let me give another example of a problematic comparison function that seems to
  79. >me to satisfy all the requirements of the standard, but leads, I think, to
  80. >unintended problems for implementing qsort.
  81. >
  82. >The comparison function returns 1 if the first value is greater than the second
  83. >value, or -1 if the second   However, if they are equal, the function then
  84. >compares the values of the pointers to the parameters (the actual arguments of
  85. >the function) - if the first is greater, 1 is returned, if the second is
  86. > greater
  87. >-1 is returned, otherwise 0 is returned.
  88.  
  89. >This function is a (misguided) attempt to create a stable sort.  It is only a
  90. >valid comparison function if qsort() only passes pointers to elements in the
  91. >array being sorted to the comparison function - otherwise, the pointer
  92. >comparisons are (probably) undefined.
  93.  
  94. OK, however since the algorithm is unspecified you can't say any more than
  95. this results in undefined behaviour.
  96.  
  97.   This comparions function will "work"
  98. >(i.e. not produce undefined behaviour) for some possible implementations of
  99. >qsort(), but not others.  It would also produce a consistent sort order for
  100. > some
  101. >implementations, but not others.  (I think Bubble Sort could be implemented
  102. > with
  103. >these restriction.  Quicksort would be trickier, or a lot slower.)
  104.  
  105. Whether a particular qsort() 'works' or not is not important, only whether
  106. all implementations are guaranteed to work or not i.e. whether the code
  107. is strictly conforming (apart from the issue of the unspecified order for
  108. equal keys).
  109.  
  110. >So - is this comparison function legal or not?  I cannot see anything in the
  111. >standard that disallows it.  I think the requirement to avoid undefined
  112. >behaviour in this case can reasonably be interpreted as falling on qsort(), not
  113. >on the comparison function.  I am sure the committee never intended such a
  114. >think, but I the current standard can reasonably be read as containing them.
  115.  
  116. Assuming that the particular qsort() implementation does only pass
  117. pointers to the array's elements you violate the rules for the relation,
  118. certainly i) and probably ii) also. So the operation as a whole isn't a sort
  119. and the standard defines no behaviour for it.
  120.  
  121. -- 
  122. -----------------------------------------
  123. Lawrence Kirby | fred@genesis.demon.co.uk
  124. Wilts, England | 70734.126@compuserve.com
  125. -----------------------------------------
  126.